 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"
char* export_file (char* file_path,char* output_file)
{
	int index; 
	char result[100];
	//displayBstTree(bstroot);
	index=bst_search(bstroot,file_path);
	
	if(index==-1)
	{	
		strcpy(result,"exportfile_FAILURE ");
		strcat(result,ERR_VFS_EXPORTFILE_01);
		sprintf(str, "%s",result);
		return str;	
	}
	
	char *ext= strrchr(file_path,'.');
	char *str12= ext+1; 	
	
	char *ext1= strrchr(output_file,'.');
	char *str1= ext1+1; 
	if(!strcmp(fd[index].file_type,"folder"))
	{
		strcpy(result,"exportfile_FAILURE ");
		strcat(result,ERR_VFS_EXPORTFILE_03);
		sprintf(str, "%s",result);
		return str;
	}	
	int offset = main_header_var->headersize + 1024*(fd[index].block_number);
	//printf("offset in list:%d",offset);
	FILE *fout, *fp;
	fp=fopen(mounted_vfs,"rb+");
	fout=fopen(output_file,"w");	
	fseek(fp,offset,SEEK_SET);
	char buffer[1024];

	if(fp==NULL)
	{
		strcpy(result,"exportfile_FAILURE ");
		strcat(result,ERR_VFS_EXPORTFILE_01);
		sprintf(str, "%s",result);
		return str;
	}

	if(fout==NULL)
	{	
		strcpy(result,"exportfile_FAILURE ");
		strcat(result,ERR_VFS_EXPORTFILE_02);
		sprintf(str, "%s",result);
		return str;
	}

	int size= fd[index].file_size; //printf("list: %d, %d\n",index,size);
	int ch; int x;
	while((x= ftell(fout))<size)
	{		//printf("%d ",x);
			ch= fgetc(fp); //printf("%c",ch);
			fputc(ch,fout);
	}
	//fputc('EOF',fout);

	fclose(fp);
	fclose(fout);
	
	return "exportfile_SUCCESS";
}
